package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("smallVideos")
@Slf4j
public class VideoController {
     @Autowired
     private VideoService videoService;
     @PostMapping
     public ResponseEntity<Void> saveVideo(@RequestParam("videoThumbnail") MultipartFile picFile,
                                           @RequestParam("videoFile") MultipartFile videoFile){
         try {
             Boolean result= videoService.saveVideo(picFile,videoFile);
             if(result){
                 return ResponseEntity.ok(null);
             }
         } catch (Exception e) {
             e.printStackTrace();
         }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
     }
      @GetMapping
      public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                        @RequestParam(value ="pagesize",defaultValue = "10")Integer pagesize){
          try {
              if(page<=0){
                  page=1;
              }
         PageResult pageResult=videoService.queryVideoList(page,pagesize);
              if (ObjectUtil.isNotEmpty(pageResult)){
                  return ResponseEntity.ok(pageResult);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
      }
       @PostMapping("/{id}/like")
       public ResponseEntity<Long> likeComment(@PathVariable("id")String videoId){
           try {
               Long likeCount= videoService.likeComment(videoId);
               if(ObjectUtil.isNotEmpty(likeCount)){
                   return ResponseEntity.ok(likeCount);
               }
           } catch (Exception e) {
               e.printStackTrace();
           }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
       }
       @PostMapping("/{id}/dislike")
       public ResponseEntity<Long>disComment(@PathVariable("id")String videoId){
           try {
               Long likeCount= videoService.dislikeComment(videoId);
               if(ObjectUtil.isNotEmpty(likeCount)){
                   return ResponseEntity.ok(likeCount);
               }
           } catch (Exception e) {
               e.printStackTrace();
           }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
       }
       @GetMapping("/{id}/comments")
       public ResponseEntity<PageResult>queryCommentsList(@PathVariable("id")String videoId,
                                                          @RequestParam("page")Integer page,
                                                          @RequestParam("pagesize")Integer pagesize){
           try {
               PageResult pageResult =videoService.queryCommentsList(videoId,page,pagesize);
               if(ObjectUtil.isNotEmpty(pageResult)){
                   return ResponseEntity.ok(pageResult);
               }
           } catch (Exception e) {
               e.printStackTrace();
           }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
       }
        @PostMapping("/{id}/comments")
        public ResponseEntity<Void>saveComment(@RequestBody Map<String,String> parm,
                                               @PathVariable("id")String videoId){
            try {
                String content = parm.get("comment");
                Boolean result = videoService.saveComment(content,videoId);
                if (result){
                    return ResponseEntity.ok(null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String videoCommentId) {
        try {
            Long likeCount = this.videoService.likeComment(videoCommentId);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("id") String videoCommentId) {
        try {
            Long likeCount = this.videoService.dislikeComment(videoCommentId);
            if (null != likeCount) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @PostMapping("/{id}/userFocus")
    public ResponseEntity<Void>saveUserFocusComments(@PathVariable("id") Long userId){
        try {
            Boolean result=videoService.followUser(userId);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity<Void>saveUserUnFocusComments(@PathVariable("id") Long userId){
        try {
            Boolean result=videoService.disfollowUser(userId);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
